package com.leyou.user.controller;

import com.leyou.dto.AddressDTO;
import com.leyou.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.user.controller
 * @date 2019/7/6 16:32
 * @description:
 **/
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 校验用户名和手机的唯一性
     * @param data
     * @param type
     * @return
     */
    @GetMapping("check/{data}/{type}")
    public ResponseEntity<Boolean> checkUserData(
            @PathVariable("data") String data,@PathVariable("type")Integer type){
        return ResponseEntity.ok(userService.checkUserData(data,type));
    }


    /**
     * 生成短信验证码
     * @param phone
     * @return
     */
    @PostMapping("code")
    public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone){
        userService.sendCode(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 用户注册
     * @param user
     * @param code
     * @return
     */
    @PostMapping("register")
    public ResponseEntity<Void> register(User user, @RequestParam("code")String code){
        userService.register(user,code);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }


    /**
     * 根据用户名和密码查询用户
     * @param username
     * @param password
     * @return
     */
    @GetMapping("query")
    public ResponseEntity<UserDTO> query(@RequestParam("username") String username, @RequestParam("password") String password){
        return ResponseEntity.ok(userService.query(username,password));
    }


    @RestController
    @RequestMapping("address")
    public class AddressController {

        /**
         * 根据
         * @param userId 用户id
         * @param id 地址id
         * @return 地址信息
         */
        @GetMapping
        public ResponseEntity<AddressDTO> queryAddressById(@RequestParam("userId") Long userId, @RequestParam("id") Long id){
            AddressDTO address = new AddressDTO();
            address.setId(1L);
            address.setStreet("航头镇航头路18号传智播客 3号楼");
            address.setCity("上海");
            address.setDistrict("浦东新区");
            address.setAddressee("虎哥");
            address.setPhone("15800000000");
            address.setProvince("上海");
            address.setPostcode("210000");
            address.setIsDefault(true);
            return ResponseEntity.ok(address);
        }
    }

}
